
package it.bd.SetAllConnectionsOff.helper;

import android.content.Context;
import android.content.ContextWrapper;
import android.util.Log;

public class NetworkHelper extends AServiceHelper {
    public static final String   PREFS_KEY = "network";

    private static NetworkHelper instance  = null;

    public static NetworkHelper getInstance() {
        if (instance == null) {
            Log.e("", "NetworkHelper not initialized !");
        }
        return instance;
    }

    private ContextWrapper context;

    public NetworkHelper(ContextWrapper context) {
        this.context = context;
        instance = this;
    }

    @Override
    ContextWrapper getContext() {
        return context;
    }

    @Override
    String getPrefKey() {
        return PREFS_KEY;
    }

    @Override
    public boolean isServiceEnabled() {
        try {
            Object connectivityManager = getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            return (Boolean) CallerHelper.callGetter(connectivityManager, "getMobileDataEnabled");
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
        return false;
    }

    @Override
    public boolean isServicePresent() {
        
        Object connectivityManager = getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        return connectivityManager != null;
         
    }

    @Override
    public void setServiceEnabled(boolean serviceEnabled) {
        try {
            Log.i("", "Setting net " + (serviceEnabled ? "up" : "down"));
            Object connectivityManager = getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            CallerHelper.callSetter(connectivityManager, "setMobileDataEnabled", serviceEnabled);
        } catch (Exception e) {
            Log.e("", "Something went bad setting mobileData's state", e);
        }
    }

}
